package frame.laxcen.response;

import frame.laxcen.BaseFrame;
import frame.laxcen.dataobject.DataObject_0000_00;
import frame.laxcen.response.annotation.FrameFeature;
import frame.laxcen.util.FrameReader;
import frame.laxcen.exception.IllegalFrameFormatException;

import java.util.HashMap;
import java.util.Map;

// 返回读写器对错误的反馈信息
@FrameFeature(MessageType = 0b0000, Mid = 0x0)
public class Response_0000_00 extends BaseResponse {

    public Response_0000_00(BaseFrame baseFrame) throws IllegalFrameFormatException {

        super(baseFrame);

        getContentMap().putAll(explainToMap());
    }

    @Override
    protected Map<String, String> analyzeResponse(FrameReader reader) throws IllegalFrameFormatException {

        Map<String, String> result = new HashMap<>();

        switch(reader.readU8()) {

            case 0: result.put("错误类型", "错误的类型号"); break;
            case 1: result.put("错误类型", "CRC校验错误"); break;
            case 2: result.put("错误类型", "错误的MID"); break;
            case 3: result.put("错误类型", "协议控制字其他错误"); break;
            case 4: result.put("错误类型", "当前状态无法执行该指令"); break;
            case 5: result.put("错误类型", "指令队列已满"); break;
            case 6: result.put("错误类型", "消息参数不完整"); break;
            case 7: result.put("错误类型", "帧长度超过限制"); break;
            case 8: result.put("错误类型", "其他错误"); break;
        }

        // 读写器状态

        switch(reader.readU8()) {

            case 0: result.put("读写器状态", "空闲状态"); break;
            case 1: result.put("读写器状态", "执行状态"); break;
            case 2: result.put("读写器状态", "故障状态"); break;
        }

        // 收到的协议控制字

        String mid = "0x" + Integer.toHexString(Short.toUnsignedInt(reader.readU16()));

        result.put("收到的协议控制字", mid);

        // 收到的消息内容长度

        String len = String.valueOf(Short.toUnsignedInt(reader.readU16()));

        result.put("收到的消息内容长度", len);

        return result;
    }

    public DataObject_0000_00 getDataObject() {

        return new DataObject_0000_00(getContentMap());
    }
}
